package com.eboy.testcamera;

import java.io.File;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends Activity {
	private SurfaceView surfaceView;
	private MediaRecorder mediaRecorder;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        
        surfaceView = (SurfaceView)this.findViewById(R.id.SurfaceView);
        surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        surfaceView.getHolder().setFixedSize(800, 480);
        surfaceView.getHolder().setKeepScreenOn(true);
    }  
    
    public void SheXiang(View v){
    	Button btnSheXiang = (Button)findViewById(R.id.btnSheXiang);
    	if (btnSheXiang.getText().equals(getString(R.string.SheXiang))) {
    		btnSheXiang.setText(getString(R.string.stopSheXiang));
    		
    		File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis() + ".3gp");
    		mediaRecorder = new MediaRecorder();
    		mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    		mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    		mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    		mediaRecorder.setVideoSize(800, 480);
    		mediaRecorder.setVideoFrameRate(15);
    		mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
    		mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    		mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
    		mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
    		try {
    			mediaRecorder.prepare();
    			mediaRecorder.start(); 
    		} catch (Exception E){
    			E.printStackTrace();
    		}    				
    	} else {    	
    		btnSheXiang.setText(getString(R.string.SheXiang));
    		mediaRecorder.stop();
    		mediaRecorder.release();
    		mediaRecorder = null;
    	}
    }
}
